<template>
  <div id="articles" class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row>
          <el-form :inline="true" :model="query" label-width="80px">
            <el-col :span="22">
              <el-form-item label="关键字" class="Keyword">
                <el-input
                  clearable
                  v-model="query.keyword"
                  placeholder="根据文章标题搜索"
                ></el-input>
              </el-form-item>
              <el-form-item label="状态" class="state">
                <el-select v-model="query.state" clearable placeholder="请选择">
                  <el-option
                    v-for="item in status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" @click="cleartext">清除</el-button>
                <el-button type="primary" size="small" @click="searchText"
                  >搜索</el-button
                >
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-button
                @click="addDept"
                type="success"
                icon="el-icon-edit"
                size="small"
                >新增技巧</el-button
              >
            </el-col>
          </el-form>
        </el-row>
        <!-- 提示信息 -->
        <el-alert :title="`数据一共${total}条`" type="info" show-icon>
        </el-alert>
        <br />
        <el-table :data="rolelist" style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column label="文章标题" width="400px">
            <template #default="{ row }">
            {{ row.title }}
            <a
              v-if="row.videoURL"
              href="#"
              @click.prevent="openVideo(row.videoURL)"
              ><i class="el-icon-film"
            /></a>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数"> </el-table-column>
          <el-table-column prop="username" label="录入人"> </el-table-column>
          <el-table-column prop="createTime" label="录入时间">
            <template slot-scope="scope">
              {{ scope.row.createTime | formTime }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" width="100px">
            <template slot-scope="scope">{{
              scope.row.state | formStart
            }}</template>
          </el-table-column>
          <el-table-column label="操作" width="160px">
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row)"
                type="text"
                size="small"
                >预览</el-button
              >
              <el-button type="text" size="small" @click="open(scope.row)">{{
                scope.row.state === 1 ? "禁用" : "启用"
              }}</el-button>
              <el-button
                :disabled="scope.row.state == 1 ? false : true"
                type="text"
                size="small"
                @click="updateId(scope.row.id)"
                >修改</el-button
              >
              <el-button
                :disabled="scope.row.state == 1 ? false : true"
                type="text"
                size="small"
                @click="removeId(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <br />
        <div class="card">
          <el-pagination
            class="Paging"
            background
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            :current-page="query.page"
            :total="total"
            layout=" prev, pager, next,sizes, jumper"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="query.pagesize"
          >
          </el-pagination>
        </div>
              <!-- 视频 -->
      <div v-if="videoURL" class="videoPlay">
        <div class="close" @click="closeVideo()"><span class="el-icon-close" /></div>
        <div class="videobox"><video ref="videoRef" :src="videoURL" /></div>
      </div>
      </el-card>
      <!-- 新增/修改 -->
      <el-dialog
        v-if="showDialog"
        :isEdit="isEdit"
        :title="isEdit ? '修改文章' : '添加文章'"
        :visible.sync="showDialog"
        width="60%"
      >
        <el-form
          ref="addEmpRef"
          :model="formData"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文章标题" prop="title">
            <el-input
              v-model="formData.title"
              style="width: 100%"
              placeholder="请输入文章标签"
            />
          </el-form-item>
          <el-form-item label="文章内容" prop="articleBody">
            <quillEditor
              v-model="formData.articleBody"
              :options="editorOption"
            ></quillEditor>
          </el-form-item>
          <el-form-item label="视频地址" prop="videoURL">
            <el-input
              v-model="formData.videoURL"
              style="width: 100%"
              placeholder="请输入视频地址"
            />
          </el-form-item>
          <el-form-item>
            <el-button class="Btn" type="primary" @click="onSubmit">确 定</el-button>
            <el-button class="Btn" @click="showDialog = false">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 预览 -->
      <el-dialog :model="roleData" title="预览文章" :visible.sync="dialogVisible" width="800px">
<h2>{{ roleData.title }}</h2>
      <div class="title">
        <span>{{ roleData.createTime |formTime }} </span>
        <span>{{ roleData.username }}</span>
        <span class="el-icon-view"></span>
        <span>{{ roleData.visits }}</span>
      </div>
      <div class="info"></div>
      <div class="articleBody" v-html="roleData.articleBody" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  list,
  add,
  remove,
  update,
  detail,
  changeState
} from '../../api/hmmm/articles'
import dayjs from 'dayjs'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { status } from '../../api/hmmm/constants'
export default {
  name: 'Articles',
  components: {
    quillEditor
  },
  data () {
    return {
      // 表单对象
      formData: {
        title: '',
        articleBody: null,
        videoURL: ''
      },
      // 预览对象
      roleData: {},
      // 下拉框选项
      status,
      // 校验规则
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      // 弹框是否显示
      showDialog: false,
      // 分页
      query: {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      },
      // 文章列表
      rolelist: [],
      // 总条数
      total: 0,
      // 视频地址
      videoURL: null,
      // 富文本配置对象
      editorOption: {},
      isEdit: false, // 是否是修改
      parentId: '',
      dialogVisible: false // 预览弹框
    }
  },
  created () {
    this.list()
  },
  methods: {
    // 关闭预览弹框
    closePreview () {
      this.dialogVisible = false
    },
    // 点击视频按钮播放
    openVideo (url) {
      this.videoURL = url
      this.$nextTick(() => {
        this.$refs.videoRef.play()
      })
    },
    // 点击关闭按钮视频暂停
    closeVideo () {
      this.$refs.videoRef.pause()
      this.videoURL = null
    },

    // 启用禁用
    async open (row) {
      if (row.state === 1) {
        row.state = 0
      } else if (row.state === 0) {
        row.state = 1
      }
      await changeState({ id: row.id, state: row.state })
      this.$message.success('操作成功')
    },

    // 预览
    async handleClick (row) {
      this.roleData = row
      this.dialogVisible = true
    },

    // 添加文章按钮
    addDept () {
      this.isEdit = false
      this.showDialog = true
    },

    // 修改文章按钮
    async updateId (id) {
      const res = await detail({ id })
      this.parentId = id
      this.formData = res.data
      this.isEdit = true
      this.showDialog = true
    },

    // 添加/修改文章弹框
    async onSubmit () {
      this.$refs.addEmpRef.validate(async (valid) => {
        if (!valid) return
        if (this.isEdit) {
          // 编辑部门
          const params = { ...this.formData, id: this.parentId }
          await update(params)
        } else {
          // 添加
          await add(this.formData)
        }
        this.$message.success('操作成功')
        // 重新获取列表
        this.list()
        // 关闭弹框
        this.showDialog = false
        this.formData = {
          title: '',
          articleBody: null,
          videoURL: ''
        }
      })
    },

    // 删除文章
    async removeId (id) {
      const confirmRes = await this.$confirm(
        '此操作将永久删除该文章, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmRes === 'cancel') return this.$message('您取消了删除')
      await remove({ id })
      // 重新获取列表
      this.list()
      // 给用户删除提示
      this.$message.success('删除成功')
    },

    // 搜索按钮
    searchText () {
      this.query.page = 1
      this.list()
    },

    // 清除按钮
    cleartext () {
      this.query = {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      }
    },

    // 文章列表
    async list () {
      const res = await list(this.query)
      this.rolelist = res.data.items
      this.total = res.data.counts
    },

    handleCurrentChange (newPage) {
      this.query.page = newPage
      this.list()
    },

    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      this.list()
    }
  },

  filters: {
    formStart (val) {
      const obj = status.find((item) => item.value === val)
      if (obj) {
        return '已' + obj.label
      } else {
        return '未知'
      }
    },

    formTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped>
.el-icon-film{
  color:blue
}
  h2 {
    margin: 0;
    padding: 0;
  }
  .info {
    padding-bottom: 10px;
  }
  span {
    margin-left: 10px;
    margin-top: 10px;
  }
  .articleBody {
    background-color: #f5f5f5;
    border-top: 1px dashed #ccc;
    padding: 10px;
  }
  .videoPlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    overflow: hidden;
    z-index: 9999;
  }
    .close {
      position: absolute;
      top: 30px;
      left: 50%;
      transform: translateX(-50%);
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.4);
      line-height: 50px;
      text-align: center;
      font-size: 20px;
      color: #fff;
    }
    .videobox {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 800px;
      height: 600px;
    }
      video {
        width: 100%;
        height: 100%;
      }
.Btn{
  float: right;
  margin-right: 10px;
}
::v-deep .ql-editor {
  height: 200px;
}
.card {
  display: flex;
}
.Paging {
  flex: 1;
  text-align: right;
}
.Keyword {
  margin-left: 30px;
}
.state {
  margin-left: 30px;
}

</style>
